#include <iostream>
#include "../httplib.h"
#include "./as_control.hpp"
using namespace httplib;
using namespace as_control;

int main()
{
    Server svr;
    // http://39.104.201.129:8080/
    control ctrl;

    svr.Get("/analysis", [&ctrl](const Request &req, Response &resp)
            { 
                /*分析情感的html网页*/
                std::string html;
                ctrl.analysis_text(&html);
                resp.set_content(html, "text/html;charset=utf-8"); });
    svr.Post(R"(/language)", [&ctrl](const Request &req, Response &resp)
             {
        std::string result_json;
        ctrl.analysis(req.body,&result_json);
        resp.set_content(result_json,"application/json;charset=utf-8"); });

    svr.Post(R"(/search)", [&ctrl](const Request &req, Response &resp)
             {
        std::string result_json;
        ctrl.search_user(req.body,&result_json);
        resp.set_content(result_json,"application/json;charset=utf-8"); });

    svr.Post(R"(/import)", [&ctrl](const Request &req, Response &resp)
             {
        std::string result_json;
        ctrl.import_corpus(req.body,&result_json);
        resp.set_content(result_json,"application/json;charset=utf-8"); });

    svr.Post(R"(/modify)", [&ctrl](const Request &req, Response &resp)
             {
        std::string result_json;
        ctrl.modify_corpus(req.body,&result_json);
        resp.set_content(result_json,"application/json;charset=utf-8"); });

    svr.Post(R"(/query)", [&ctrl](const Request &req, Response &resp)
             {
        std::string result_json;
        ctrl.query_corpus(req.body,&result_json);
        resp.set_content(result_json,"application/json;charset=utf-8"); });

    //1.对前端传来的请求进行相应，传递后端相应控制模块
    //7.从out_json串中拿到查询到的语料，反馈回前端界面
    svr.Post(R"(/querycorpus)", [&ctrl](const Request &req, Response &resp)
        {
        std::string result_json;
        ctrl.query_modelcorpus(req.body,&result_json);
        resp.set_content(result_json,"application/json;charset=utf-8"); });

    svr.Post(R"(/modifycorpus)", [&ctrl](const Request &req, Response &resp)
        {
        std::string result_json;
        ctrl.modify_modelcorpus(req.body,&result_json);
        resp.set_content(result_json,"application/json;charset=utf-8"); });

    svr.Post(R"(/createuser)", [&ctrl](const Request &req, Response &resp)
        {                                     
        std::string result_json;
        ctrl.root_createuser(req.body,&result_json);
        resp.set_content(result_json,"application/json;charset=utf-8"); });

    svr.Post(R"(/deleteuser)", [&ctrl](const Request &req, Response &resp)
        {                                     
        std::string result_json;         
        ctrl.root_deleteuser(req.body,&result_json);
        resp.set_content(result_json,"application/json;charset=utf-8"); });


    svr.set_base_dir("../wwwroot");
    svr.listen("0.0.0.0", 8080);
    return 0;
}
